<template>
  <div>
      <div class="title-msg">类目添加</div>

    <div class="option-site">
      <div class="back" @click="backClick">《返回</div>
      <div class="root-category">
        <div class="root-category-title">主类目一览</div>
        <div class="root-category-option-site">
          <div class="root-category-item" v-for="i in root_navs">
            <abbr :title="i.describe"><span>{{i.name}}</span></abbr>
          </div>
        </div>
      </div>

      <div class="goods-category">
        <div class="goods-category-title">商品类目管理</div>

        <div class="goods-category-option-site">
          <div class="goods-category-item">
            <span>类目名称:</span>
            <OrdInput :p-input-style="{'width': '250px', 'height': '30px'}"
                      class="input"
                      v-model="goods_category_name"
                      @mBlur="gcnVerBlur"
            ></OrdInput>
          </div>
          <div class="goods-category-item">
            <span>所属类目:</span>
            <select v-model="root_navs_id">
              <option v-for="i in root_navs" :value="i.id">{{i.name}}</option>
            </select>

          </div>

          <div class="goods-category-item">
            <span>描述信息:</span>
            <textarea @blur="gcdVerBlur" v-model="goods_category_describe"></textarea>
          </div>

          <div class="goods-category-item">
            <button class="goods-category-add btn" @click="goodCategoryAddClick">商品类目添加</button>
          </div>

        </div>

      </div>

    </div>

  </div>
</template>

<script>
  // 引入公共组件
  import NumInput from 'components/common/input/numInput/IInput'
  import OrdInput from 'components/common/input/ordInput/IInput'
  import ImgUpload from "components/common/upload/ImgUpload";

  import {addRootCategory} from 'network/admin'
  import {setGoodsCategory} from 'network/mix'

  import {mapGetters} from "vuex";
  import {verToken} from 'common/utils'

  export default {
    name: "CategoryAdd",
    components: {
      NumInput,
      OrdInput,
      ImgUpload,
    },
    data() {
      return {
        root_navs: [], // 获取root类目
        root_navs_id: 8, // 当前选中的root类目,默认为8,即数据库的 其他 主类目选项

        root_category_name: '', // 主类目的名字
        root_category_describe: '', // 主类目的描述

        goods_category_name: '', // 商品类目的名字
        goods_category_describe: '', // 商品类目的描述
        goods_category_index: 1, // 商品类目的热度

        rcnVer: false, // 主类目的名字验证
        rcdVer: false, // 主类目的描述验证
        gcnVer: false, // 商品类目的名字验证
        gciVer: false, // 商品类目的热度验证

        isDescribeShow: false, // 商品类目描述是否可见
      }
    },
    computed: {
      ...mapGetters ({
        token: 'getToken'
      })
    },
    mounted() {
      // console.log('------------mounted=');
      this.root_navs = this.$route.params.rootCategory // 获取路由中的参数,内含有主类目信息
      // console.log(this.root_navs);
    },
    methods: {
      backClick() {
        this.$router.go(-1);
      },

      rcnBlur() { // 主类目名字
        if (this.root_category_name.length > 0) {
          this.rcnVer = true
        } else {
          this.rcnVer = false;
          this.$err_ty_toast.toShow('主类目名字输入为空！！！', 2000);
        }
      },
      rcdBlur() {
        if (this.root_category_describe.length > 0) {
          this.rcdVer = true
        } else {
          this.rcdVer = false
          this.$err_ty_toast.toShow('主类目描述输入为空！！！', 2000);
        }
      },
      rootCategoryAddClick() {
        if (this.rcnVer && this.rcdVer) {
          // 添加主类目
          this.mAddRootCategory()
        } else {
          this.$err_ty_toast.toShow('主类目输入存在问题！！！', 2000);
        }
      },

      gcnVerBlur() {
        if (this.goods_category_name.length > 0) {
          this.gcnVer = true
        } else {
          this.gcnVer = false
          this.$err_ty_toast.toShow('商品类目名字输入为空！！！', 2000)
        }
      },
      gcdVerBlur() {
        if (this.goods_category_describe.length > 0) {
          this.gcdVer = true
        } else {
          this.gcdVer = false
          this.$err_ty_toast.toShow('商品类目描述为空！！！', 2000)
        }
      },
      goodCategoryAddClick() {
        if (this.gcnVer && this.gcdVer) {
          this.mSetGoodsCategory()
        } else {
          this.$err_ty_toast.toShow('商品类目输入存在问题！！！', 2000);
        }
      },

      /**axios添加主类目的网络请求方法*/
      mAddRootCategory() {
        addRootCategory(this.root_category_name, this.root_category_describe)
          .then(res => {
            // console.log(res);
            this.$succ_ty_toast.toShow('添加主类目' + this.root_category_name + '成功', 3000)
            this.root_category_name = ''
            this.root_category_describe = ''
          })
        .catch(error => {
          this.$err_ty_toast.toShow('添加主类目' + this.root_category_name + '失败！！！', 3000)
        })
      },
      mSetGoodsCategory() { // 添加商品类目的axios网络请求方法
        setGoodsCategory(this.goods_category_name, this.goods_category_describe, this.root_navs_id, verToken(this.token).username, this.goods_category_index)
          .then(res => {
            // console.log(res);
            this.$succ_ty_toast.toShow('添加商品类目' + this.goods_category_name + '成功', 3000)
            this.goods_category_name = ''
            this.goods_category_describe = ''
            this.root_navs_id = 1
            this.goods_category_index = 1
          })
        .catch(error => {
          console.log(error);
          this.$err_ty_toast.toShow('添加商品类目' + this.goods_category_name + '失败！！！', 2000)
        })
      }

    }
  }
</script>

<style scoped>
  .title-msg {
    height: 60px;
    line-height: 60px;
    margin-left: 20px;
    margin-right: 20px;
    border-bottom-right-radius: 6px;
    border-bottom-left-radius: 6px;
    border: 1px solid #cdcdcd;

    text-align: center;
    font-size: 30px;

    background-color: white;
  }
  .option-site {
    position: relative;
    margin-top: 30px;
    margin-left: 20px;
    margin-right: 20px;
    height: 530px;

    border: 1px solid #cdcdcd;
    border-radius: 6px;
    background-color: #ebebeb;

    /*background-color: green;*/
  }
  .option-site .back {
    position: absolute;
    width: 50px;
    height: 25px;

    color: #919191;
    font-weight: bold;
    font-size: 16px;

    background-color: #fff;
    border-top-left-radius: 6px;
  }
  .option-site .back:hover {
    cursor: pointer;
    color: black;
    opacity: 0.75;
  }

  .root-category {
    width: 50%;
    height: 100%;

    border-right: 2px solid #cdcdcd;

    /*background-color: #fff;*/
  }
  .root-category-title {
    font-size: 20px;
    text-align: center;
    font-weight: bold;

    /*background-color: red;*/
  }
  .root-category-option-site {
    width: 100%;
    height: 95%;

    /*background-color: yellow;*/
  }
  .root-category-option-site .root-category-item {
    margin-bottom: 10px;
    position: relative;

    text-align: center;

    /*background-color: yellow;*/
  }
  .root-category-option-site .root-category-item span {
    color: #575757;
    font-weight: bold;
    font-size: 20px;
  }

  .btn {
    width: 250px;
    height: 40px;

    color: white;
    font-size: 20px;

    outline: none;
    border: none;
    border-radius: 10px;
    background-color: #28a745;
  }
  .btn:hover {
    opacity: 0.75;
    cursor: pointer;
  }

  .goods-category {
    position: absolute;
    top: 0;
    right: 0;
    width: 50%;
    height: 100%;

    border-left: 2px solid #cdcdcd;

    /*background-color: yellow;*/
  }
  .goods-category-title {
    font-size: 20px;
    text-align: center;
    font-weight: bold;

    /*background-color: red;*/
  }
  .goods-category-option-site {
    width: 100%;
    height: 95%;

    /*background-color: green;*/
  }
  .goods-category-option-site .goods-category-item {
    position: relative;
    height: 10%;
    width: 70%;

    transform: translateX(30%);
    margin-top: 8%;
    margin-bottom: 10%;

    /*background-color: yellow;*/
  }
  .goods-category-option-site .goods-category-item span {
    font-weight: bold;
    color: #575757;
    font-size: 20px;
  }
  .goods-category-option-site .goods-category-item select {
    width: 150px;
    height: 30px;
    margin-right: 10px;
  }
  .goods-category-option-site .goods-category-item textarea {
    position: absolute;
    height: 100%;
    width: 60%;

    outline: none;
    border: none;
  }
  .goods-category-option-site .goods-category-item textarea:focus {
    border: 2px solid rgba(85, 167, 255, 0.6);
  }

  .goods-category-option-site .goods-category-item .goods-category-add {
    margin-left: 20%;
  }
</style>